<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 0px;
            top: 0px
        }

        .box2 {
            width: 100px;
            height: 100px;
            background: blue;
            position: absolute;
            left: 300px;
            top: 0px
        }
    </style>
</head>

<body>
    <div class="box1"></div>
    <div class="box2"></div>

    <script>
        var box1ele = document.querySelector(".box1");
        var box2ele = document.querySelector(".box2");
        box1ele.onmousedown = function (event) {
            var e = event || window.event;
            var x1 = e.clientX;
            var y1 = e.clientY;
            this.onmousemove = function (event) {
                var e = event || window.event;
                var x2 = e.clientX;
                var y2 = e.clientY;
                this.style.left = x2 - x1 + "px";
                this.style.top = y2 - y1 + "px";
                // 元素碰撞方式一
                // if (Math.abs(box2ele.offsetLeft - this.offsetLeft) < this.offsetWidth &&
                //     Math.abs(box2ele.offsetTop - this.offsetTop) < this.offsetHeight) {
                //     console.log("x轴碰上了");
                // }

                // 元素碰撞方式二
                // var AminX = box1ele.offsetLeft;
                // var AmaxX = box1ele.offsetLeft + box1ele.offsetWidth;
                // var AminY = box1ele.offsetTop;
                // var AmaxY = box1ele.offsetTop + box1ele.offsetHeight;

                // var BminX = box2ele.offsetLeft;
                // var BmaxX = box2ele.offsetLeft + box2ele.offsetWidth;
                // var BminY = box2ele.offsetTop;
                // var BmaxY = box2ele.offsetTop + box2ele.offsetHeight;

                // if (AmaxX > BminX && AminX < BmaxX && AmaxY > BminY && AminY < BmaxY) {
                //     console.log("x轴上碰撞了");
                // }


            }

            document.onmouseup = function () {
                box1ele.onmousemove = "";
            }
            var maxArae = 0;


            crashArr.forEach(function (item) {
                // var w = Math.abs( that.offsetLeft + that.offsetWidth - item.offsetLeft);
                // var h =   Math.abs( that.offsetTop + that.offsetHeight - item.offsetTop);

                var w = that.offsetWidth - Math.abs(that.offsetLeft - item.offsetLeft);
                var h = that.offsetHeight - Math.abs(that.offsetTop - item.offsetTop);
                // console.log(h);
                var arae = w * h;
                // console.log(arae);
                if (arae > maxArae) {
                    maxArae = arae;
                    maxEle = item;
                }
            })
            // console.log(maxEle);

        }
        document.onmouseup = function () {
            // 拖动结束；
            item.onmousemove = "";
            item.style.zIndex = 0;
            // console.log(obj);
            // 把div还原 到初始位置；
            item.style.left = maxEle.style.left;
            item.style.top = maxEle.style.top;
            maxEle.style.transition = "all 1s";
            maxEle.style.left = obj.left;
            maxEle.style.top = obj.top;
            item.style.transition = "all 1s";
        }
}
        }
    </script>
</body>

</html>